package controls
{
	import com.demonsters.debugger.MonsterDebugger;
	
	import feathers.controls.Button;
	import feathers.core.PopUpManager;
	
	import flash.filesystem.File;
	
	import logic.LogicBarleybreak;
	
	import starling.animation.Transitions;
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	import ui.popups.WinPopup;
	
	import utils.LoaderResource;
	
	public class GameField extends Sprite
	{
		public static const UPDATE_COUNTER:String = "updateCounter";
		private var imgFon:Image;
		private var ceilsAr:Array;
		private var arField:Array;
		private var tween:Tween;
		public function GameField()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		private function init():void{
			imgFon = new Image(LoaderResource.instance.assetManager.getTexture("Background"));
			addChild(imgFon);
			imgFon.x = (stage.stageWidth-imgFon.width)/2+30>>1;
			imgFon.y = (stage.stageHeight-imgFon.height)/2+30>>1;
			LogicBarleybreak.getInstance();
			addCeils();
		}
		
		private function addCeils():void{
			ceilsAr = new Array();
			var step_x:int=imgFon.x+20;
			var step_y:int=imgFon.y+20;
			arField = LogicBarleybreak.getInstance().getRandomMatrix();
			for (var j:int = 0; j < arField.length; j++) 
			{
				for (var k:int = 0; k < arField[j].length; k++) 
				{
					var ceil:GameCell = new GameCell();
					if(arField[j][k]){
						ceil.setNumber(arField[j][k]);
						ceil.x = step_x;
						ceil.y = step_y;
						addChild(ceil);
						ceil.addEventListener(TouchEvent.TOUCH, onClickCeil);
						ceilsAr.push(ceil);
						
					}
					step_x+= 45;
				}
				step_x = imgFon.x+20;
				step_y += 45;
				
			}
			
		}
		
		private function onClickCeil(e:TouchEvent):void{
			e.stopPropagation();
			if(tween && !tween.isComplete){
				return;
			}
			if(e.touches[0].phase == TouchPhase.ENDED){
				var ceil:GameCell = e.currentTarget as GameCell;
//				MonsterDebugger.trace("e.currentTarget as GameCell",ceil);
				var move:int = LogicBarleybreak.getInstance().getMoveCell(ceil.numderCeil,arField);
//				MonsterDebugger.trace("move"+move,move);
				if(move!=0){
					var i:int = 0;
					var j:int = 0;
					var x:int=0;
					var y:int =0;
					for (i = 0; i < arField.length; i++) 
					{
						for (j = 0; j < arField[i].length; j++) 
						{
							if(arField[i][j] == ceil.numderCeil){
								x=i;
								y=j;
								break;
							}
						}
						
					}
//					MonsterDebugger.trace("arField i->"+i+" j->"+j,arField);
//					MonsterDebugger.trace("arField x->"+x+" y->"+y,arField);
					if(move==-1){
						arField[x][y-1] = ceil.numderCeil;
					}
					if(move== 1){
						arField[x][y+1] = ceil.numderCeil;
					}
					if(move==2){
						arField[x+1][y] = ceil.numderCeil;
					}
					if(move==-2){
						arField[x-1][y] = ceil.numderCeil;
					}
					arField[x][y] = 0;
					tween = new Tween(e.currentTarget,0.5);
					switch(move)
					{
						case -1:
						{
							tween.animate("x",(ceil.x-ceil.width-5));
							break;
						}
							
						case 1:
						{
							tween.animate("x",(ceil.x+ceil.width+5));
							break;
						}
						case 2:
						{
							tween.animate("y",(ceil.y+ceil.height+5));
							break;
						}
						case -2:
						{
							tween.animate("y",(ceil.y-ceil.height-5));
							break;
						}
					}
					Starling.juggler.add(tween);
					if(LogicBarleybreak.getInstance().isWin(arField)){
						PopUpManager.addPopUp(new WinPopup(),true,true);
					}
					dispatchEvent(new Event(UPDATE_COUNTER,true));
				}
			
			}
		}
		
		private function onCompleteLoad(ratio:Number):void{
			if(ratio == 1){
				init();
			}
			
		}
		private function addToStage(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			LoaderResource.instance.assetManager.enqueue(File.applicationDirectory.resolvePath("../bin-debug/Art/Background.png"));
			LoaderResource.instance.assetManager.loadQueue(onCompleteLoad);
		}
	}
}